package com.itheima.web.manager;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.itheima.domain.db.Admin;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.service.db.AdminService;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.db.UserService;
import com.itheima.util.ConstantUtil;
import com.itheima.util.JwtUtil;
import com.itheima.web.exception.BusinessException;
import com.itheima.web.interceptor.AdminHolder;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;

@Service
public class AdminManager {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    /**
     * 生成验证码
     * @param uuid
     * @return
     */
    public LineCaptcha createImgCode(String uuid) {
        //生成图片
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(300, 100);

        //获得验证码
        String code = lineCaptcha.getCode();
        System.out.println(code);
        //将验证码放入缓存中 以便于校验验证码
        stringRedisTemplate.opsForValue().set(ConstantUtil.ADMIN_CODE +  uuid ,code  , Duration.ofMinutes(5)); //5分钟过期
        return lineCaptcha;
    }

    @Reference
    private AdminService adminService;
    /**
     * 登录
     */
    public Map login(String username, String password, String verificationCode, String uuid) {
        //1.从redis中取出验证码
        String redisCode = stringRedisTemplate.opsForValue().get(ConstantUtil.ADMIN_CODE + uuid);
        //2.对比验证码
        if(!StrUtil.equals(verificationCode , redisCode)){
            throw new BusinessException("对不起,验证码错误");
        }

        //3.根据名称查询admin
        Admin admin = adminService.findByName(username);
        if(admin==null){
            throw new BusinessException("对不起,用户名输入错误");
        }
        //4.校验密码
        String md5Pwd = SecureUtil.md5(password);
        if(!StrUtil.equals(md5Pwd ,admin.getPassword() )){
            throw new BusinessException("对不起,密码输入错误");
        }
        //5.构建token
        Map<String, String> map = new HashMap<>();
        map.put("id" , admin.getId() +"");
        map.put("username" , admin.getUsername());
        String token = JwtUtil.createToken(map);
        //6.将用户登录信息保存到redis
        stringRedisTemplate.opsForValue().set(ConstantUtil.ADMIN_TOKEN + token , "1" ,Duration.ofMinutes(30) );

        //7.构建返回值
        Map<String, String> returnMap = new HashMap<>();
        returnMap.put("token" ,token );

        //删除验证码
        stringRedisTemplate.delete(ConstantUtil.ADMIN_CODE + uuid);
        return returnMap;
    }

    /**
     * 校验用户的登录状态
     */
    public Map profile() {
        /*//1.获得redis的token
        Boolean flag = stringRedisTemplate.hasKey(ConstantUtil.ADMIN_TOKEN + token);
        if(!flag){
            throw  new BusinessException("登录状态异常");
        }
        //2.解析
        Map<String,String> map = JwtUtil.parseToken(token);
        String username = map.get("username");*/

        Admin tokenAdmin = AdminHolder.get();
        //3.根据username查询数据
        Admin admin = adminService.findByName(tokenAdmin.getUsername());
        //4.构建返回值
        Map returnMap = new HashMap();
        returnMap.put("uid", admin.getId());
        returnMap.put("username", admin.getUsername());
        returnMap.put("avatar", admin.getAvatar());
        return returnMap;
    }

    /**
     * 登出
     * @param token
     * @return
     */
    public Map logout(String token) {
        //删除redis的缓存状态
        stringRedisTemplate.delete(ConstantUtil.ADMIN_TOKEN + token);
        return null;
    }



}
